package com.gsm.phimhanhdong;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.database.Cursor;
import android.os.IBinder;

import com.gsm.phimhanhdong.ContentNetworkInterface.GetListPlaylistForCheckUpdateSuccessEvent;
import com.gsm.phimhanhdong.DatabaseHelper.PlaylistDetailTable;

import de.greenrobot.event.EventBus;

public class CheckNewPhotoService extends Service {

	public static final String ACTIOn = "com.gsm.phimhanhdong";
	private NotificationManager mNotificationManager;
	BackgroundTasks bgTasks;
	int current_count ;
	int update_count;

	@Override
	public void onCreate() {
		super.onCreate();
		// System.exit(0);
		EventBus.getDefault().register(this);
		bgTasks = new BackgroundTasks(getApplicationContext());
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}
	
	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		if (intent != null) {
			String action = intent.getAction();
			if (getSharedPreferences("videoyoutubeappota", 0).getBoolean(
					"notification", true)) {
				if (action.equals(ACTIOn)) {
					boolean isstop = intent.getBooleanExtra("stop", false);
					if (isstop) {
						stopSelf();
						return;
					}
					current_count = 0;
					Cursor c = getContentResolver().query(
							PhimhanhdongContentProvider.CONTENT_URI, null,
							null, null, null);
					if (c.getCount()>0) {
						c.moveToFirst();
						do{
							current_count += Integer
									.valueOf(c.getString(c
											.getColumnIndex(PlaylistDetailTable.Column.PLAYLIST_COUNT)));
						}while(c.moveToNext());
					}else{
						stopSelf();
						return;
					}
					c.close();
					if (current_count > 0) {
						bgTasks.getPlaylistForCheckUpdate(MenuFragment.chanel_id);
					}else{
						stopSelf();
					}
				}

			} else {
				stopSelf();
			}

		}else{
			stopSelf();
		}
	}

	public void onEventMainThread(GetListPlaylistForCheckUpdateSuccessEvent e) {
		update_count = 0;
		if (e.playlists.size() > 0) {
			for (Playlist playlist : e.playlists) {
				update_count += playlist.item_count;
			}
			if (update_count > current_count) {
				foregroundNotification(update_count -current_count);
				stopSelf();
			} else {
				stopSelf();
			}
		} else {
			stopSelf();
		}
	}

	protected void foregroundNotification(int count) {
		Notification notification = new Notification(R.drawable.vlog,
				"Có " + count + " phim mới", System.currentTimeMillis());
		notification.flags = Notification.FLAG_AUTO_CANCEL;
		Intent notificationIntent = new Intent(this, HomeActivity.class);
		notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
				notificationIntent, 0);
		notification.setLatestEventInfo(this,
				getResources().getString(R.string.app_name), "Vừa có "
						+ count + " phim mới rất hay.", pendingIntent);
		mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		mNotificationManager.notify(6969, notification);
	}
}
